package {
  import __AS3__.vec.Vector;
  import com.bit101.components.Label;
  import flash.events.FocusEvent;
  import flash.events.MouseEvent;
  import mge.Ease;
  
  import flash.display.Sprite;
  import flash.events.Event;
  import flash.display.StageScaleMode;
  import flash.display.StageAlign;
  
  import mge.ui.Button;

  public class Buttons extends Sprite
  {
    [Embed(source="C:/lib/mge/projects/buttons/res/1.png")]
    private var r1:Class;
    [Embed(source="C:/lib/mge/projects/buttons/res/2.png")]
    private var r2:Class;
    [Embed(source="C:/lib/mge/projects/buttons/res/3.png")]
    private var r3:Class;
    [Embed(source="C:/lib/mge/projects/buttons/res/4.png")]
    private var r4:Class;
    
    [Embed(source="C:/lib/mge/projects/buttons/res/1_16.png")]
    private var r1_16:Class;
    [Embed(source="C:/lib/mge/projects/buttons/res/2_16.png")]
    private var r2_16:Class;
    [Embed(source="C:/lib/mge/projects/buttons/res/3_16.png")]
    private var r3_16:Class;
    [Embed(source="C:/lib/mge/projects/buttons/res/4_16.png")]
    private var r4_16:Class;
    
    
    private var cont:ButtonContainer;
    private const NUM_BUTTONS:int = 4
    private var label:Label;
    private var focusMsg:Label;
    
    private var lockers:Vector.<Button> = new Vector.<Button>();
    
    public function Buttons()
    {
      // var btn:mge.ui.Button = new mge.ui.Button(new r2, new r4, new r1, new r3, this);
      stage.scaleMode = StageScaleMode.NO_SCALE;
      stage.align = StageAlign.TOP_LEFT;
      
      cont = new ButtonContainer(this);
      cont.rearrangeFunction = rearrangeLine;
      
      var x:int = Math.random() * 300;
      for (var i:int = 0; i < NUM_BUTTONS; i++)
      {
        // Enabled
        var enabled:Boolean = Math.random() > 0.3;
        // Adds button
        var btn:mge.ui.Button = new mge.ui.Button("button_" + i, new r2, new r4, new r1, new r3, this);
        btn.enabled = enabled;
        btn.addOnClickCallback(OnClickButton);
        
        // Adds locker
        var locker:Button = new Button("locker_" + i, new r2_16, new r4_16, new r1_16, new r3_16, this);
        locker.addOnClickCallback(OnClickButton);
        locker.enabled = enabled;
        lockers.push(locker);
        
        cont.addButton(btn);
      }
      
      for (i = 0; i < NUM_BUTTONS; i++)
      {
          btn = cont.getButton("button_" + i);
          if (btn.enabled)
          {
              btn.selected = true;
              break;
          }
      }
      
      label = new Label(this, 150, 10);
      // btn.enable = false;
      focusMsg = new Label(this, 150, 150, "Click Here to allow flash get focus!");
      
      stage.addEventListener(MouseEvent.CLICK, OnFocusIn);
    }
    
    private function OnFocusIn(e:Event):void
    {
        focusMsg.text = "";
    }
    
    private function OnClickButton(btnName:String):void
    {
      for (var i:int = 0; i < 4; i++)
      {
        if (btnName == "locker_" + i)
        {
            var b:Button = cont.getButton("button_" + i);
            b.keyboardNavigable = !b.keyboardNavigable;
            
            var locker:Button = lockers[i];
            locker.selected = !b.keyboardNavigable;
        }
      }
      /*
      if (btnName == "button_3")
      {
        // cont.rearrangeFunction = rearrange;
        for (var i:int = 0; i < NUM_BUTTONS; i++)
        {
            var btn:Button = cont.getButton("button_" + i);
            btn.enabled = false;
        }
      }*/
      
      /*
      if (btnName == "button_2")
      {
        cont.rearrangeFunction = rearrangeSquare;
      }*/
      label.text = btnName + " pressed!";
    }
    
    private function rearrange(buttons:Vector.<Button>):void
    {        
        if (buttons.length == 0) return;
        
        var length:int = buttons.length;
        
        var sep_x:Number = 1 / (buttons.length + 1);
        var sep_y:Number = 250 / (length);
        var e:Ease = new Ease(Ease.cubicIn_01);
        
        for (var i:int = 0; i < buttons.length; i++)
        {
            var b:Button = buttons[i];                
            b.x = x + 80 * e.delta(sep_x * (buttons.length - i), 1);
            b.y = y + sep_y * i;
        }
        
    }
    
    private function rearrangeSquare(buttons:Vector.<Button>):void
    {
        if (buttons.length == 0) return;
        
        var columns:int = 10
        
        for (var i:int = 0; i < NUM_BUTTONS; i++)
        {
            buttons[i].x = (i % columns) * 80;
            var y:int = i / columns;
            buttons[i].y = y * 80;
        }
    }
    
    private function rearrangeLine(buttons:Vector.<Button>):void
    {
        if (buttons.length == 0) return;
        for (var i:int = 0; i < buttons.length; i++)
        {
            var y:Number = i / buttons.length;
            buttons[i].y = y * 270;
            lockers[i].x = buttons[i].x + 64;
            lockers[i].y = buttons[i].y + 50;
        }
    }
    
  }
}
